﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using Autodesk.Revit.DB;
using DataBase;

namespace DBService
{
	public class DbColumn : DbObject
	{
		[Category("属性")]
		[Description("截面")]
		[DisplayName("截面")]
		[ReadOnly(true)]
		public SectionParameter SectionParameter
		{
			get
			{
				return this.mParameter;
			}
			set
			{
				this.mParameter = value;
			}
		}

		[Category("属性")]
		[Description("基点")]
		[DisplayName("基点")]
		[ReadOnly(true)]
		public XYZ Location
		{
			get
			{
				return this.mPt;
			}
			set
			{
				this.mPt = value;
			}
		}

		[Category("属性")]
		[Description("高度")]
		[DisplayName("高度(mm)")]
		[ReadOnly(true)]
		public double Height
		{
			get
			{
				return this.mHeight;
			}
			set
			{
				this.mHeight = value;
			}
		}

		[Category("属性")]
		[Description("建筑模型Id")]
		[DisplayName("建筑模型Id")]
		[ReadOnly(true)]
		public int RacId
		{
			get
			{
				return this.mRacId;
			}
			set
			{
				this.mRacId = value;
			}
		}

		[Category("属性")]
		[Description("结构模型Id")]
		[DisplayName("结构模型Id")]
		[ReadOnly(true)]
		public int RstId
		{
			get
			{
				return this.mRstId;
			}
			set
			{
				this.mRstId = value;
			}
		}

		[Category("属性")]
		[Description("角度")]
		[DisplayName("角度")]
		[ReadOnly(true)]
		public double Angle
		{
			get
			{
				return this.mAngle;
			}
			set
			{
				this.mAngle = value;
			}
		}

		[Category("属性")]
		[Description("建筑模型路径")]
		[DisplayName("建筑模型路径")]
		[ReadOnly(true)]
		public string ArchPath
		{
			get
			{
				return DbProject.GetFromDB(this.mProjectId, BimServerDB.Default).RacFilePath;
			}
		}

		[Category("属性")]
		[Description("结构模型路径")]
		[DisplayName("结构模型路径")]
		[ReadOnly(true)]
		public string RstPath
		{
			get
			{
				return DbProject.GetFromDB(this.mProjectId, BimServerDB.Default).RstFilePath;
			}
		}

		public DbColumn(int projectId, int racId, int rstId, XYZ pt, double height, double angle, SectionParameter sParam)
		{
			this.mProjectId = projectId;
			this.mPt = pt;
			this.mParameter = sParam;
			this.mRacId = racId;
			this.mRstId = rstId;
			this.mHeight = height;
			this.mAngle = angle;
		}

		public static DbColumn GetFromDB(int id, IDatabase db)
		{
			DbColumn result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT ProjectId,RacID,RstID,PtX,PtY,PtZ,Angle,Height,SectionType,Parameter1,Parameter2 FROM tbcolumn WHERE ID={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					int projectId = Convert.ToInt32(dataTable.Rows[0]["ProjectId"]);
					int racId = Convert.ToInt32(dataTable.Rows[0]["RacID"]);
					int rstId = Convert.ToInt32(dataTable.Rows[0]["RstID"]);
					XYZ pt = new XYZ(Convert.ToDouble(dataTable.Rows[0]["PtX"]), Convert.ToDouble(dataTable.Rows[0]["PtY"]), Convert.ToDouble(dataTable.Rows[0]["PtZ"]));
					double angle = Convert.ToDouble(dataTable.Rows[0]["Angle"]);
					double height = Convert.ToDouble(dataTable.Rows[0]["Height"]);
					DBService.SectionType sectionType = (DBService.SectionType)Convert.ToInt32(dataTable.Rows[0]["SectionType"]);
					SectionParameter sParam = null;
					if (sectionType == DBService.SectionType.Arc)
					{
						sParam = new SectionParameter(Convert.ToDouble(dataTable.Rows[0]["Parameter1"]));
					}
					else if (sectionType == DBService.SectionType.Rect)
					{
						double param = Convert.ToDouble(dataTable.Rows[0]["Parameter1"]);
						double param2 = Convert.ToDouble(dataTable.Rows[0]["Parameter2"]);
						sParam = new SectionParameter(param, param2);
					}
					result = new DbColumn(projectId, racId, rstId, pt, height, angle, sParam)
					{
						mId = id,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbColumn> GetFromDB(IDatabase db)
		{
			List<DbColumn> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbColumn> list = new List<DbColumn>();
				string sql = string.Format("SELECT Id,ProjectId,RacID,RstID,PtX,PtY,PtZ,Angle,Height,SectionType,Parameter1,Parameter2 FROM tbcolumn", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = list;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int mId = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						int projectId = Convert.ToInt32(dataTable.Rows[i]["ProjectId"]);
						int racId = Convert.ToInt32(dataTable.Rows[i]["RacID"]);
						int rstId = Convert.ToInt32(dataTable.Rows[i]["RstID"]);
						XYZ pt = new XYZ(Convert.ToDouble(dataTable.Rows[i]["PtX"]), Convert.ToDouble(dataTable.Rows[i]["PtY"]), Convert.ToDouble(dataTable.Rows[i]["PtZ"]));
						double angle = Convert.ToDouble(dataTable.Rows[i]["Angle"]);
						double height = Convert.ToDouble(dataTable.Rows[i]["Height"]);
						DBService.SectionType sectionType = (DBService.SectionType)Convert.ToInt32(dataTable.Rows[i]["SectionType"]);
						SectionParameter sParam = null;
						if (sectionType == DBService.SectionType.Arc)
						{
							sParam = new SectionParameter(Convert.ToDouble(dataTable.Rows[i]["Parameter1"]));
						}
						else if (sectionType == DBService.SectionType.Rect)
						{
							double param = Convert.ToDouble(dataTable.Rows[i]["Parameter1"]);
							double param2 = Convert.ToDouble(dataTable.Rows[i]["Parameter2"]);
							sParam = new SectionParameter(param, param2);
						}
						list.Add(new DbColumn(projectId, racId, rstId, pt, height, angle, sParam)
						{
							mId = mId,
							mDatabase = db
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static void Clear(DbProject project)
		{
			foreach (DbColumn dbColumn in DbColumn.GetByProject(project))
			{
				dbColumn.RemoveFromDb();
			}
		}

		public static List<DbColumn> GetByProject(DbProject project)
		{
			List<DbColumn> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbColumn> list = new List<DbColumn>();
				string sql = string.Format("SELECT Id,ProjectId,RacID,RstID,PtX,PtY,PtZ,Angle,Height,SectionType,Parameter1,Parameter2 FROM tbcolumn WHERE ProjectId={0}", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int mId = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						int projectId = Convert.ToInt32(dataTable.Rows[i]["ProjectId"]);
						int racId = Convert.ToInt32(dataTable.Rows[i]["RacID"]);
						int rstId = Convert.ToInt32(dataTable.Rows[i]["RstID"]);
						XYZ pt = new XYZ(Convert.ToDouble(dataTable.Rows[i]["PtX"]), Convert.ToDouble(dataTable.Rows[i]["PtY"]), Convert.ToDouble(dataTable.Rows[i]["PtZ"]));
						double angle = Convert.ToDouble(dataTable.Rows[i]["Angle"]);
						double height = Convert.ToDouble(dataTable.Rows[i]["Height"]);
						DBService.SectionType sectionType = (DBService.SectionType)Convert.ToInt32(dataTable.Rows[i]["SectionType"]);
						SectionParameter sParam = null;
						if (sectionType == DBService.SectionType.Arc)
						{
							sParam = new SectionParameter(Convert.ToDouble(dataTable.Rows[i]["Parameter1"]));
						}
						else if (sectionType == DBService.SectionType.Rect)
						{
							double param = Convert.ToDouble(dataTable.Rows[i]["Parameter1"]);
							double param2 = Convert.ToDouble(dataTable.Rows[i]["Parameter2"]);
							sParam = new SectionParameter(param, param2);
						}
						list.Add(new DbColumn(projectId, racId, rstId, pt, height, angle, sParam)
						{
							mId = mId,
							mDatabase = project.Database
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbcolumn (SectionType, Parameter1,Parameter2,RacID,RstID,PtX,PtY,PtZ,Angle,Height,ProjectId) VALUES ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10})", new object[]
				{
					(int)this.mParameter.SectionType,
					this.mParameter.Parameter1,
					this.mParameter.Parameter2,
					this.mRacId,
					this.mRstId,
					this.mPt.X,
					this.mPt.Y,
					this.mPt.Z,
					this.mAngle,
					this.mHeight,
					this.mProjectId
				});
				db.ExecuteCommand(sql);
				string sql2 = string.Format("select max(Id) from tbcolumn", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbcolumn SET SectionType = {0},Parameter1={1},Parameter2={2},RacID={3},RstID={4},PtX={5},PtY={6},PtZ={7},Angle={8},Height={9},ProjectId={10} WHERE Id ={11}", new object[]
				{
					this.mParameter.SectionType,
					this.mParameter.Parameter1,
					this.mParameter.Parameter2,
					this.mRacId,
					this.mRstId,
					this.mPt.X,
					this.mPt.Y,
					this.mPt.Z,
					this.mAngle,
					this.mHeight,
					this.mProjectId,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbcolumn WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override string ToString()
		{
			return string.Format("结构({0})-建筑({1})", this.mRstId.ToString(), this.mRacId.ToString());
		}

		private int mProjectId;

		private SectionParameter mParameter;

		private XYZ mPt;

		private double mHeight;

		private int mRacId;

		private int mRstId;

		private double mAngle;
	}
}
